<?php
/**
 * 项目：红河移动电路资源管理信息系统
 *
 * 公司：红河移动分公司
 *
 * @author 李朝应
 *
 * E-mail:lichaoying.hh@163.com
 *
 */
class User_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }


   function setUser($org3_id,$name,$email,$tel,$login,$pw,$lev){
    $data = array(
               'org3_id' => $org3_id,
               'us_name' => $name,
               'us_email' => $email,
               'us_tel' => $tel,
               'us_login' => $login,
               'us_pw' => md5(md5($pw,16)),
               'level_str' => $lev,
            );

    $this->db->insert('user', $data);
    return $this->db->affected_rows() ;

   }

 function setPwd($login,$pw){
   $data = array(
                'us_pw' => md5(md5($pw,16)),
            );
   $this->db->where('us_login',$login);
   $this->db->update('user', $data);
   return $this->db->affected_rows() ;
 }
 function updateuser($id,$org3_id,$name,$email,$tel,$login,$pw,$lev){
    $data = array(
               'org3_id' => $org3_id,
               'us_name' => $name,
               'us_email' => $email,
               'us_tel' => $tel,
               'us_login' => $login,
               'us_pw' => md5(md5($pw,16)),
               'level_str' => $lev,
            );
      $this->db->where('us_id',$id);
      $this->db->update('user', $data);
    return $this->db->affected_rows() ;
 }
function delUser($id){
    $this->db->delete('user', array('us_id' => $id));
	return true;
 }

function getCount(){
      return  $this->db->count_all("user");
   }

 function getUserAll(){
      $sql="SELECT * FROM crms_user, crms_org3 WHERE crms_user.org3_id = crms_org3.org3_id";
      $query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			 return $query; //返回查询结果
        }
      }
    function getUserBy($id){
     $this->db->where("us_id", $id);
     $query = $this->db->get('user');
		if ($query->num_rows() > 0)
		{
			 return $query->row_array(); //返回查询结果
        }
   }
   function getUserByName($name){
        $query = $this->db->get('user');
        $sql="SELECT * FROM crms_user, crms_org3
              WHERE crms_user.org3_id = crms_org3.org3_id
              AND(crms_user.us_name like'%$name%' OR crms_user.us_login like'%$name%')";
        $query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			 return $query; //返回查询结果
        }
   }
   function hasUserName($org3_id,$name){
        $this->db->where("org3_id", $org3_id);
        $this->db->where("us_name", $name);
		$query = $this->db->get('user');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
   function hasUserLogin($login){
        $this->db->where("us_login", $login);
		$query = $this->db->get('user');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
   function getUserNameAuth($name){
        $this->db->select("us_id as id ,us_name as name");
        $this->db->where("us_name", $name);
		$query = $this->db->get('user');
        if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
 function getLoginNameAuth($name){
   $this->db->select("us_id as id ,us_login as name");
        $this->db->where("us_login", $name);
		$query = $this->db->get('user');
        if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
 }
  function getUserNameUpdateAuth($id,$name){
        $this->db->select("us_id as id ,us_name as name");
        $this->db->where("us_name", $name);
        $this->db->where('us_id !=', $id);
		$query = $this->db->get('user');
        if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
 function getLoginNameUpdateAuth($id,$name){
   $this->db->select("us_id as id ,us_login as name");
        $this->db->where("us_login", $name);
        $this->db->where('us_id !=', $id);
		$query = $this->db->get('user');
        if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
 }
 function isSysytemUser($user,$pwd){
        $this->db->where("us_login", $user);
        $this->db->where("us_pw", md5(md5($pwd,16)));
		$query = $this->db->get('user');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
 }
 function getUserNameInfo($login){
      $sql="SELECT crms_user.us_id, crms_org1.org1_id,crms_org1.org1_name, crms_org2.org2_id,crms_org2.org2_name, crms_org3.org3_id, crms_org3.org3_name, crms_user.us_name
            FROM crms_user, crms_org3, crms_org2, crms_org1
            WHERE (
            crms_user.org3_id = crms_org3.org3_id
            AND crms_org3.org2_id = crms_org2.org2_id
            AND crms_org2.org1_id = crms_org1.org1_id
               )
            AND us_login=$login ";
      $query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			 return $query->row_array(); //返回查询结果
        }

 }
 function getUserWord($login){
    $sql="SELECT Receipt_word as word
            FROM crms_user, crms_org3, crms_org2, crms_org1
            WHERE (
            crms_user.org3_id = crms_org3.org3_id
            AND crms_org3.org2_id = crms_org2.org2_id
            AND crms_org2.org1_id = crms_org1.org1_id
               )
            AND us_login='$login' ";
      $query = $this->db->query($sql);
    foreach ($query->result_array() as $row)
    {
     $word=$row['word'];
     }
   return $word;//返回调单字

 }
 function getUserByLogin($login){
   $this->db->where('us_login',$login);
    $query = $this->db->get('user');
		if ($query->num_rows() > 0)
		{
			 return $query->row_array(); //返回查询结果
        }

 }
  function getUserOrg1ID($login){
    $sql="SELECT  crms_org1.org1_id as id
            FROM crms_user, crms_org3, crms_org2, crms_org1
            WHERE (
            crms_user.org3_id = crms_org3.org3_id
            AND crms_org3.org2_id = crms_org2.org2_id
            AND crms_org2.org1_id = crms_org1.org1_id
               )
            AND us_login='$login' ";
      $query = $this->db->query($sql);
    foreach ($query->result_array() as $row)
    {
     $id=$row['id'];
     }
   return $id;//返回调单字

 }
}//类定义结束
?>
